﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>

<script src="/sites/contosobeta/Style%20Library/JS/jquery-1.8.1.js"></script>
<script src="/sites/contosobeta/Style%20Library/JS/RESTApiHelper.js"></script>
<script>
    var siteUrl = _spPageContextInfo.webAbsoluteUrl;
    $(document).ready(function () {
        //SP.SOD.executeFunc("SP.js","SP.ClientContext",CallREST);	
        $('#btnSiteCreate').click(function () {
            var Title = $('#txtSTitle').val();
            var Description = $('#txtSDescription').val();
            var Url = $('#txtSUrl').val();
            var WebTemplate = parseInt($('#txtSWebTemplate').val());
            var UseUniquePermissions = $('#txtSUseUniquePermissions').val() == "True" ? true : false;


            var restWeb = new RESTApiHelper.Web(siteUrl);
            restWeb.CreateSubsite(Title, Url, Description, WebTemplate, UseUniquePermissions, doSuccess,doError);

        });

        $('#btnSiteProperty').click(function () {
            var Property = $('#txtSProperty').val();

            var restWeb = new RESTApiHelper.Web(siteUrl);
            restWeb.GetWebProperty(Property, doSuccess, doError);
        });

        $('#btnGetSite').click(function () {
            //var Property= $('#txtSProperty').val();

            var restWeb = new RESTApiHelper.Web(siteUrl);
            restWeb.GetWeb(doSuccess,doError);
        });

        $('#btnCreateGroup').click(function () {
            var GroupName = $('#txtSProperty').val();

            var restWeb = new RESTApiHelper.Web(siteUrl);
            var restGroups = new restWeb.Groups();

            restGroups.CreateGroup(GroupName, doSuccess, doError);

        });


        $('#btnCreate').click(function () {
            var Title = $('#txtCTitle').val();
            var Description = $('#txtCDescription').val();
            var restWeb = new RESTApiHelper.Web(siteUrl);
            restWeb.CreateList(Title, Description, doSuccess, doError);
        });

        $('#btnGetList').click(function () {
            var Title = $('#txtGTitle').val();

            var restWeb = new RESTApiHelper.Web(siteUrl);
            var myList = new restWeb.List(Title);
            myList.GetList(doSuccess,doError);

        });

        $('#btnUpdate').click(function () {
            var Title = $('#txtUTitle').val();
            var NTitle = $('#txtUNewTitle').val();
            var NDescription = $('#txtUNewDescription').val();

            var restWeb = new RESTApiHelper.Web(siteUrl);
            var myList = new restWeb.List(Title);
            myList.UpdateList(NTitle, NDescription, doSuccess, doError);

        });

        $('#btnDeleteList').click(function () {
            var Title = $('#txtDTitle').val();

            var restWeb = new RESTApiHelper.Web(siteUrl);
            var myList = new restWeb.List(Title);
            myList.DeleteList(doSuccess,doError);
        });

        $('#btnGetListItem').click(function () {
            var Title = $('#txtLITitle').val();
            var ID = $('#txtLIID').val();
            var Filter = $('#txtLIFilter').val();
            var Select = $('#txtLISelect').val();

            var restWeb = new RESTApiHelper.Web(siteUrl);
            var myList = new restWeb.List(Title);
            var items = new myList.Items();
            if (ID != '')
                items.GetItemByID(ID, doSuccess, doError);
            else
                items.GetItems(Select, Filter, doSuccess, doError);
        });
        $('#btnAddListItem').click(function () {
            var ListTitle = $('#txtLITitle').val();
            var Title = $('#txtLIID').val();

            var restWeb = new RESTApiHelper.Web(siteUrl);
            var myList = new restWeb.List(ListTitle);
            var items = new myList.Items();
            items.AddItem(Title, doSuccess, doError);
        });


        $('#btnCallRest').click(function () {
            CallREST();
        });

    });

    function CallREST() {
        var URL = $('#txtURL').val();
        $.ajax({
            url: URL,
            type: "GET",
            headers: { "accept": "application/json;odata=verbose" },
            success: doSuccess,
            error: doError
        });
    }

    function doSuccess(data) {
        /*var emailId=[];
        $.each(data.d.results,function(){
            $.each(this.TargetUsers.results, function(){
                emailId.push(this.EMail);
            });
        });
        $('#result').text(emailId.join(';'));*/

        $('#result').text(JSON.stringify(data));
    }

    function doError(err) {
        alert(JSON.stringify(err));
    }



</script>

<h3>Call REST URL</h3>
<table>
<tr><td>URL</td><td><input id="txtURL" type="text"></td></tr>
<tr><td></td><td><input id="btnCallRest" type="button" value="Call REST"></td></tr>
</table>
<hr/>


<h3>Create Site</h3>
<table>
<tr><td>Title</td><td><input id="txtSTitle" type="text"></td></tr>
<tr><td>Description</td><td><input id="txtSDescription" type="text"></td></tr>
<tr><td>Url</td><td><input id="txtSUrl" type="text"></td></tr>
<tr><td>WebTemplate</td><td><input id="txtSWebTemplate" type="text"></td></tr>
<tr><td>UseUniquePermissions</td><td><input id="txtSUseUniquePermissions" type="text"></td></tr>
<tr><td></td><td><input id="btnSiteCreate" type="button" value="Create Site"></td></tr>
</table>
<hr/>
<h3>Site Properties</h3>
<table>
<tr><td>Property Name</td><td><input id="txtSProperty" type="text"></td></tr>
<tr><td></td><td><input id="btnSiteProperty" type="button" value="Site Property">
<input id="btnGetSite" type="button" value="Get Site">
<input id="btnCreateGroup" type="button" value="Create Group">
</td></tr>
</table>
<hr/>

<h3>Create List</h3>
<table>
<tr><td>Title</td><td><input id="txtCTitle" type="text"></td></tr>
<tr><td>Description</td><td><input id="txtCDescription" type="text"></td></tr>
<tr><td></td><td><input id="btnCreate" type="button" value="Create List"></td></tr>
</table>
<hr/>
<h3>Get List</h3>
<table>
<tr><td>Title</td><td><input id="txtGTitle" type="text"></td></tr>
<tr><td></td><td><input id="btnGetList" type="button" value="Get List"></td></tr>
</table>
<hr/>
<h3>Update List</h3>
<table>
<tr><td>Title</td><td><input id="txtUTitle" type="text"></td></tr>
<tr><td>New Title</td><td><input id="txtUNewTitle" type="text"></td></tr>
<tr><td>New Description</td><td><input id="txtUNewDescription" type="text"></td></tr>
<tr><td></td><td><input id="btnUpdate" type="button" value="Update List"></td></tr>
</table>
<hr/>
<h3>Delete List</h3>
<table>
<tr><td>Title</td><td><input id="txtDTitle" type="text"></td></tr>
<tr><td></td><td><input id="btnDeleteList" type="button" value="Delete List"></td></tr>
</table>
<hr/>
<h3>Get List Items</h3>
<table>
<tr><td>Title</td><td><input id="txtLITitle" type="text"></td></tr>
<tr><td>ID</td><td><input id="txtLIID" type="text"></td></tr>
<tr><td>Filter</td><td><input id="txtLIFilter" type="text"></td></tr>
<tr><td>Select</td><td><input id="txtLISelect" type="text"></td></tr>
<tr><td></td><td><input id="btnGetListItem" type="button" value="Get List Items"><input id="btnAddListItem" type="button" value="Add List Items"></td></tr>
</table>

<hr/>
<h3>Results:</h3>
<div id="result"></div>
</body>
</html>
